﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OplDataEntry.Models;

namespace OplDataEntry.Controllers
{ 
    public class FixtureController : Controller
    {
        private ORITAEntities db = new ORITAEntities();

        //
        // GET: /Fixture/

        public ViewResult Index()
        {
            var fixtures = db.Fixtures.Include("Crew").Include("Season").Include("Team").Include("Team1").Include("Team2").Include("Team3").Include("Team4");
            return View(fixtures.ToList());
        }

        //
        // GET: /Fixture/Details/5

        public ViewResult Details(long id)
        {
            Fixture fixture = db.Fixtures.Single(f => f.Id == id);
            return View(fixture);
        }

        //
        // GET: /Fixture/Create

        public ActionResult Create()
        {
            ViewBag.ManOfTheMatch = new SelectList(db.Crews, "ID", "Name");
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName");
            ViewBag.TeamID1 = new SelectList(db.Teams, "ID", "TeamName");
            ViewBag.TeamID2 = new SelectList(db.Teams, "ID", "TeamName");
            ViewBag.TeamWon = new SelectList(db.Teams, "ID", "TeamName");
            ViewBag.FirstBattingTeam = new SelectList(db.Teams, "ID", "TeamName");
            ViewBag.SecondBattingTeam = new SelectList(db.Teams, "ID", "TeamName");
            return View();
        } 

        //
        // POST: /Fixture/Create

        [HttpPost]
        public ActionResult Create(Fixture fixture)
        {
            if (ModelState.IsValid)
            {
                db.Fixtures.AddObject(fixture);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ManOfTheMatch = new SelectList(db.Crews, "ID", "Name", fixture.ManOfTheMatch);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", fixture.SeasonId);
            ViewBag.TeamID1 = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamID1);
            ViewBag.TeamID2 = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamID2);
            ViewBag.TeamWon = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamWon);
            ViewBag.FirstBattingTeam = new SelectList(db.Teams, "ID", "TeamName", fixture.FirstBattingTeam);
            ViewBag.SecondBattingTeam = new SelectList(db.Teams, "ID", "TeamName", fixture.SecondBattingTeam);
            return View(fixture);
        }
        
        //
        // GET: /Fixture/Edit/5
 
        public ActionResult Edit(long id)
        {
            Fixture fixture = db.Fixtures.Single(f => f.Id == id);
            ViewBag.ManOfTheMatch = new SelectList(db.Crews, "ID", "Name", fixture.ManOfTheMatch);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", fixture.SeasonId);
            ViewBag.TeamID1 = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamID1);
            ViewBag.TeamID2 = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamID2);
            ViewBag.TeamWon = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamWon);
            ViewBag.FirstBattingTeam = new SelectList(db.Teams, "ID", "TeamName", fixture.FirstBattingTeam);
            ViewBag.SecondBattingTeam = new SelectList(db.Teams, "ID", "TeamName", fixture.SecondBattingTeam);
            return View(fixture);
        }

        //
        // POST: /Fixture/Edit/5

        [HttpPost]
        public ActionResult Edit(Fixture fixture)
        {
            if (ModelState.IsValid)
            {
                db.Fixtures.Attach(fixture);
                db.ObjectStateManager.ChangeObjectState(fixture, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ManOfTheMatch = new SelectList(db.Crews, "ID", "Name", fixture.ManOfTheMatch);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", fixture.SeasonId);
            ViewBag.TeamID1 = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamID1);
            ViewBag.TeamID2 = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamID2);
            ViewBag.TeamWon = new SelectList(db.Teams, "ID", "TeamName", fixture.TeamWon);
            ViewBag.FirstBattingTeam = new SelectList(db.Teams, "ID", "TeamName", fixture.FirstBattingTeam);
            ViewBag.SecondBattingTeam = new SelectList(db.Teams, "ID", "TeamName", fixture.SecondBattingTeam);
            return View(fixture);
        }

        //
        // GET: /Fixture/Delete/5
 
        public ActionResult Delete(long id)
        {
            Fixture fixture = db.Fixtures.Single(f => f.Id == id);
            return View(fixture);
        }

        //
        // POST: /Fixture/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {            
            Fixture fixture = db.Fixtures.Single(f => f.Id == id);
            db.Fixtures.DeleteObject(fixture);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}